package com.example.java2e.Controller;

import com.example.java2e.Entity.Site;
import com.example.java2e.Service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RouteController {
    @Autowired
    private RouteService routeService;
    @PostMapping("/add_route")
    public Map<String, Object> add_route(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        try{
            int id_route = Integer.parseInt(front_map.get("route_id").toString());
            List<Map> sites = (List<Map>) front_map.get("sites");
            int l = sites.size();
            for(int i=0;i<l;i++){
                if(routeService.search_site_by_name((String) sites.get(i).get("name"))!=null){
                    back_map.put("code","站点名"+sites.get(i).get("name")+"重复！");
                    return back_map;
                }
            }
            for(int i=0;i<l;i++){
                routeService.add_route(id_route, (String) sites.get(i).get("name"), i+1);
            }
            back_map.put("code",'1');
            return back_map;
        }catch (Exception e){
            back_map.put("code",e.toString());
            return back_map;
        }
    }
    @PostMapping("/delete_route")
    public Map<String, Object> delete_route(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        try {
            long id_route = Long.parseLong(front_map.get("route_id").toString());
            routeService.delete_route(id_route);
            List<Long> ids = routeService.check_route();
            for(long id : ids){
                back_map.put(String.valueOf(id),id);
            }
            return back_map;
        }catch (Exception e){
            back_map.put("code",e.toString());
            return back_map;
        }
    }
    @PostMapping("/change_route_info")
    public Map<String, Object> change_route_info(@RequestBody Map<String, Object> front_map){
        delete_route(front_map);
        return add_route(front_map);
    }
    @PostMapping("/check_route_manager")
    public Map<String, Object> check_route_manager(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        List<Long> ids = routeService.check_route();
        for(long id : ids){
            back_map.put(String.valueOf(id),id);
        }
        return back_map;
    }
    @PostMapping("/check_route_user")
    public Map<String, Object> check_route_user(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        List<Long> ids = routeService.check_route();
        for(long id : ids){
            List<String> list = new ArrayList<>();
            List<Site> sites = routeService.check_route_detail(id);
            for(Site s : sites){
               list.add(s.getName());
            }
            back_map.put(String.valueOf(id),list);
        }
        return back_map;
    }
    @PostMapping("/check_route_detail")
    public Map<String, Object> check_route_detail(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        try {
            int id = Integer.parseInt(front_map.get("route_id").toString());
            List<Site> sites = routeService.check_route_detail(id);
            for(Site s : sites){
                back_map.put(s.getPosition().toString(),s.getName());
            }
        }catch (Exception e){
            back_map.put("code",e.toString());
        }
        return back_map;
    }
    @PostMapping("/search_route_keyword")
    public Map<String, Object> route(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        String keyword = front_map.get("keyword").toString();
        if(keyword.matches("[0-9]+")){
            List<String> list = new ArrayList<>();
            List<Site> sites = routeService.check_route_detail(Integer.parseInt(keyword));
            for(Site s : sites){
                list.add(s.getName());
            }
            back_map.put(keyword,list);
        }else{
            List<Long> ids = routeService.check_route();
            for(long i : ids){
                if(routeService.check_route_detail(i).size()>0){
                    List<String> list = new ArrayList<>();
                    List<Site> sites = routeService.search_route_keyword(keyword);
                    for(Site s : sites){
                        if(s.getId_route()==(i)){
                            list.add(s.getName());
                        }
                    }
                    if(list.size()>0) back_map.put(String.valueOf(i),list);
                }
            }
        }
        return back_map;
    }


}
